//
//  UIImage.swift
//  ai_assettool
//
//  Created by 李旭峰 on 2021/4/8.
//

import UIKit
import FCUUID
import Foundation

extension UIImage {
  @objc func saveImage(didCompleted:@escaping (_ imagePath:String?,_ error:Error?)->()){
    let tempImage:UIImage = self
    
    guard let imageData = tempImage.jpegData(compressionQuality: 1.0) else {
      let error = NSError(domain: "asman", code: -10000, userInfo: ["message":"图片不存在"])
      didCompleted(nil,error)
      return
    }
    let imageName = FCUUID.uuid()!
    let tempPath = NSTemporaryDirectory().appending("\(String(describing: imageName)).jpeg")
    do {
      try imageData.write(to: URL(fileURLWithPath: tempPath))
      print("file path : \(tempPath)")
      didCompleted(tempPath,nil)
    }catch let err {
      didCompleted(nil,err)
    }
  }
}
